package com.bdmc.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.bdmc.base.domain.BaseDevice;
import com.bdmc.base.domain.BasePerson;
import com.bdmc.base.service.IBaseDeviceService;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.base.domain.bo.BaseFacilityBo;
import com.bdmc.base.domain.vo.BaseFacilityVo;
import com.bdmc.base.domain.BaseFacility;
import com.bdmc.base.mapper.BaseFacilityMapper;
import com.bdmc.base.service.IBaseFacilityService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设施管理Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Service
public class BaseFacilityServiceImpl extends ServicePlusImpl<BaseFacilityMapper, BaseFacility, BaseFacilityVo> implements IBaseFacilityService {
    @Autowired
    IBaseDeviceService deviceService;

    @Override
    public BaseFacilityVo queryById(Long facilityId){
        return getVoById(facilityId);
    }

    @Override
    public TableDataInfo<BaseFacilityVo> queryPageList(BaseFacilityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseFacility> lqw = buildQueryWrapper(bo);
        Page<BaseFacilityVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<BaseFacilityVo> mySelectPage(BaseFacilityBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<BaseFacilityVo> queryList(BaseFacilityBo bo) {
        LambdaQueryWrapper<BaseFacility> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<BaseFacility> buildQueryWrapper(BaseFacilityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseFacility> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, BaseFacility::getDeptId, bo.getDeptId());
        lqw.eq(bo.getDeviceId() != null, BaseFacility::getDeviceId, bo.getDeviceId());
        lqw.like(StringUtils.isNotBlank(bo.getFacilityName()), BaseFacility::getFacilityName, bo.getFacilityName());
        lqw.eq(StringUtils.isNotBlank(bo.getFacilityNum()), BaseFacility::getFacilityNum, bo.getFacilityNum());
        lqw.eq(StringUtils.isNotBlank(bo.getFacilityType()), BaseFacility::getFacilityType, bo.getFacilityType());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BaseFacilityBo bo) {
        BaseFacility add = BeanUtil.toBean(bo, BaseFacility.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setFacilityId(add.getFacilityId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(BaseFacilityBo bo) {
        BaseFacility update = BeanUtil.toBean(bo, BaseFacility.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BaseFacility entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public Boolean bindDevice(BaseFacilityBo bo) {
        BaseFacility facility = getById(bo.getFacilityId());
        Assert.notNull(facility, "未找到待绑定的设施");
        BaseDevice device = deviceService.getById(bo.getDeviceId());
        Assert.notNull(device, "未找到指定设备");
        synchronized (this) {
            long count = count(new LambdaQueryWrapper<BaseFacility>().eq(BaseFacility::getDeviceId, bo.getDeviceId()));
            Assert.isTrue(count == 0, "设备已被绑定，请选择其他设备");
            facility.setDeviceId(bo.getDeviceId());
            updateById(facility);
            device.setBind(1);
            device.setBindTarget(facility.getFacilityName());
            deviceService.updateById(device);
            return true;
        }
    }

    @Override
    public Boolean unbindDevice(BaseFacilityBo bo) {
        BaseFacility facility = getById(bo.getFacilityId());
        Assert.notNull(facility, "未找到待绑定的设施");
        Assert.isTrue(bo.getDeviceId() != null&&bo.getDeviceId()>0 && facility.getDeviceId().equals(bo.getDeviceId()), "参数不正确");
        synchronized (this) {
            BaseDevice device = deviceService.getById(bo.getDeviceId());
            facility.setDeviceId(0L);
            updateById(facility);
            device.setBind(0);
            device.setBindTarget("");
            deviceService.updateById(device);
            return true;
        }
    }
}
